#include <iostream>

using namespace std;

double pow(double x, int n)
{
    double origin = 1;
    for (int i=0;i<n;i++)
    {
	origin *= x;
    }
    return origin;
}

double f1(double x)
{
    return pow(x,8) - 8 * pow(x,7) + 28 * pow(x,6) - 56 * pow(x,5) + 70 * pow(x,4) -
	56 * pow(x,3) + 28 * pow(x,2) - 8 * x + 1;
}

double f2(double x)
{
    return (((((((x - 8) * x + 28) * x - 56) * x + 70) * x - 56) * x + 28) * x -8) * x + 1;
}

double f3(double x)
{
    return pow(x-1,8);
}

int main()
{
    cout << "re = [" << endl;
    double x = 0.99;
    for (int i=0;i<101;i++)
    {
	x += 0.02 / 101;
	cout << x << " " << f1(x) << " " << f2(x) << " " << f3(x) << ";" << endl;
    }
    cout << "];";

    cout << "plot(re(:, 1), re(:, 2), '-r', 20);" << endl;
    cout << "axis([0.99 1.01, 0 1e-14]);" << endl;
    cout << "saveas(gcf, 'f1.png');" << endl;
    
    cout << "plot(re(:, 1), re(:, 3), '-g', 20);" << endl;    
    cout << "axis([0.99 1.01, 0 1e-14]);" << endl;
    cout << "saveas(gcf, 'f2.png');" << endl;

    cout << "plot(re(:, 1), re(:, 4), '-b');" << endl;
    cout << "axis([0.99 1.01, 0 1e-14]);" << endl;
    cout << "saveas(gcf, 'f3.png');" << endl;
    
    return 0;
}
